﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.Odbc;

namespace CocaCola_Inventory_System.Data_Access
{
    class registerUserDB
    {
        /// <summary>
        /// Adds a new user to "users" table
        /// </summary>
        /// <param name="con">Existing Connection</param>
        /// <param name="loginID">Login ID Field</param>
        /// <param name="name">Name Field</param>
        /// <param name="title">Title Field</param>
        /// <param name="email">Email Field</param>
        /// <param name="telephone">Telephone Field</param>
        /// <param name="mobile">Mobile Field</param>
        /// <param name="extensionAttr">Extension Attribute Field</param>
        /// <param name="department">Department Field</param>
        /// <param name="departmentNo">Department Number Field</param>
        /// <param name="postalAddress">Postal Address Field</param>
        /// <param name="postalCode">Postal Code Field</param>
        /// <param name="city">City Field</param>
        /// <param name="country">Country Field</param>
        /// <param name="homeDir">Home Directory Field</param>
        /// <param name="homeDrive">Home Drive Field</param>
        /// <param name="discription">Description Field</param>
        public static bool registerNewUser(OdbcConnection con,string loginID,string name,string title,string email,string telephone, string mobile, string extensionAttr,string department, string departmentNo, string postalAddress,string postalCode,string city,string country,string homeDir,string homeDrive,string description)
        {
            try
            {
                if (con == null)
                    throw new Exception("Null Connetion");

                OdbcCommand com = new OdbcCommand();
                com.Connection = con;

                com.CommandText = string.Format("SELECT * FROM users WHERE lower(loginid)=lower('{0}') OR lower(Name) = lower('{1}');",loginID, name);
                if (con.State != ConnectionState.Open)
                    con.Open();

                if (com.ExecuteNonQuery() != 0)
                {
                    throw new Exception("LoginId and Name must be unique");
                    return false;
                }

                com.CommandText = string.Format("INSERT INTO users  (Name,title,department,department_no,telephone,mobile,extension_attribute2,home_directory,home_drive,postal_address,postal_code,description,city,country,email,loginid) VALUES ('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}','{12}','{13}','{14}','{15}');", name, title, department, departmentNo, telephone, mobile, extensionAttr, homeDir, homeDrive, postalAddress, postalCode, description, city, country, email, loginID);
                if (con.State != ConnectionState.Open)
                    con.Open();

                com.ExecuteNonQuery();
                return true;
            }
            catch (Exception ex)
            {
                Common.ReportError(ex);
                return false;
            }

        }
    }
}
